﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Zone
{
    /// <summary>
    /// 用webclient载入页面，输入用户名和密码登录，或者直接载入页面获取html代码信息
    /// </summary>
    public class WebClient_LoadWeb
    {
        #region public string AspNetLogin(string s1, string s2, string uriString)
        /// <summary>
        /// UTF8下的编码 weclient方式传递用户名和密码登录页面,返回asp.net的html代码
        /// </summary>
        /// <param name="s1">用户名</param>
        /// <param name="s2">密码</param>
        /// <param name="url">页面地址</param>
        /// <returns></returns>
        public string AspNetLogin(string s1, string s2, string uriString)
        {

            string rt_s = "";

            try
            {
                WebClient webClient = new WebClient();
                byte[] responseData = webClient.DownloadData(uriString);
                string srcString = Encoding.UTF8.GetString(responseData);

                webClient = new WebClient();
                webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");


                string viewStateFlag = "id=\"__VIEWSTATE\" value=\"";
                int i = srcString.IndexOf(viewStateFlag) + viewStateFlag.Length;
                int j = srcString.IndexOf("\"", i);
                string viewState = srcString.Substring(i, j - i);

                string eventValidationFlag = "id=\"__EVENTVALIDATION\" value=\"";
                i = srcString.IndexOf(eventValidationFlag) + eventValidationFlag.Length;
                j = srcString.IndexOf("\"", i);
                string eventValidation = srcString.Substring(i, j - i);


                string submitButton = "登录";

                viewState = System.Web.HttpUtility.UrlEncode(viewState);
                eventValidation = System.Web.HttpUtility.UrlEncode(eventValidation);
                submitButton = System.Web.HttpUtility.UrlEncode(submitButton);


                //通用使用时，请修改字符串的username等
                string postString = "userName=" + s1 + "&password=" + s2 + "&loginButton=" + submitButton + "&__VIEWSTATE=" + viewState + "&__EVENTVALIDATION=" + eventValidation;
                byte[] postData = Encoding.ASCII.GetBytes(postString);
                responseData = webClient.UploadData(uriString, "POST", postData);
                srcString = Encoding.UTF8.GetString(responseData);
                rt_s = srcString;

            }
            catch (WebException we)
            {

                string msg = we.Message;
                rt_s = msg;
            }


            return rt_s;
        }
        #endregion


        #region public string JspGBK(string uriString)
        /// <summary>
        /// webClient 控件载入页面，返回页面html的代码，无验证,注意编码格式
        /// </summary>
        /// <param name="s1"></param>
        /// <param name="s2"></param>
        /// <param name="uriString"></param>
        /// <returns></returns>
        public string JspGBK(string uriString)
        {
            HttpClient webClient = new HttpClient();
            return JspGBK(uriString, ref webClient);



            //string postString = "";
            //HttpClient webClient = new HttpClient();      
            //webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            //byte[] postData = Encoding.ASCII.GetBytes(postString);
            //byte[] responseData = webClient.UploadData(uriString, "POST", postData);
            ////通用使用时，请注意调整编码GBK等
            //string srcString = Encoding.GetEncoding("GBK").GetString(responseData);
            //return srcString;
        }
       
        #endregion

        public string JspGBK(string uriString, ref HttpClient webClient)
        {
            string postString = "";            
            webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            byte[] postData = Encoding.ASCII.GetBytes(postString);
            string srcString="";
            try
            {
                byte[] responseData = webClient.UploadData(uriString, "POST", postData);
                //通用使用时，请注意调整编码GBK等
                srcString = Encoding.GetEncoding("GBK").GetString(responseData);
            }
            catch
            {
                srcString = "Error";
            }
            return srcString;
        }


        #region SetWebBrowerCookie(CookieCollection ccl,CookieContainer cct,string url) 
        /// <summary>
        /// SetWebBrowerCookie 将cookie设置到url页面中去
        /// </summary>
        /// <param name="lpszUrlName"></param>
        /// <param name="lbszCookieName"></param>
        /// <param name="lpszCookieData"></param>
        /// <returns></returns>
        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);
        public void SetWebBrowerCookie(CookieCollection ccl,CookieContainer cct,string url)
        {

            CookieContainer CookContainer = cct;

            CookieCollection CookCollection = ccl;

            string cookstr = CookieForEach.GetCookieText(CookContainer);

            //string[] cc = cookstr.Split(";".ToCharArray());

            //Uri uri = new Uri(url);

            foreach (Cookie ccc in ccl)
            {
                InternetSetCookie(url, ccc.Name, ccc.Value);
            }
        }
        #endregion
    }
}
